﻿'Imports System.IO
'Imports System.Text.Encoding
'Imports System.Security.Cryptography

''Rijndael Provider Wrapper
'Friend Class RijndaelEncryption

'#Region "Constants"

'    Dim keyStr As String = "o""°þs­<‚àÁ4¬¤™¹']‚X-LlÔXT¯_è"
'    Dim ivStr As String = "»ã¸S6 3Ê¾UîŽú"

'#End Region

'#Region "Public Variables / Properties"

'    Public Property Key() As Byte()
'        Get
'            Return System.Text.Encoding.Default.GetBytes(Me.keyStr)
'        End Get
'        Set(value As Byte())
'            Me.keyStr = System.Text.Encoding.Default.GetString(value)
'        End Set
'    End Property

'    Public Property IV() As Byte()
'        Get
'            Return System.Text.Encoding.Default.GetBytes(Me.ivStr)
'        End Get
'        Set(value As Byte())
'            Me.ivStr = System.Text.Encoding.Default.GetString(value)
'        End Set
'    End Property

'    Public Key_TripleDES() As Byte = {62, 137, 118, 185, 134, 13, 240, 240, 140, 233, 88, 112, 224, 236, 177, 60, 10, 95, 99, 55, 91, 125, 36, 62}
'    Public IV_TripleDES() As Byte = {242, 137, 225, 21, 58, 81, 97, 51}
'#End Region

'#Region "Exposed Functions"

'    Public Function encryptData_AES(rawDataStr As String) As String

'        'Convert key and iv to bytes 
'        'Dim key() As Byte = System.Text.Encoding.Default.GetBytes(keyStr)
'        'Dim iv() As Byte = System.Text.Encoding.Default.GetBytes(ivStr)

'        'Encrypt the data
'        Dim encryptedData() As Byte = encryptStringToBytes_AES(rawDataStr, Me.Key, Me.IV)

'        'Convert the encrypted data to string and return
'        Return System.Text.Encoding.UTF8.GetString(encryptedData)

'    End Function

'    Public Function decryptData_AES(encryptedDataStr As String) As String

'        'Convert key and iv to bytes 
'        Dim key() As Byte = System.Text.Encoding.Default.GetBytes(keyStr)
'        Dim iv() As Byte = System.Text.Encoding.Default.GetBytes(ivStr)
'        Dim encryptedData() As Byte = System.Text.Encoding.UTF8.GetBytes(encryptedDataStr)

'        'Decrypt the data
'        Dim decryptedData As String = decryptStringFromBytes_AES(encryptedData, Me.Key, Me.IV)

'        'return the results
'        Return decryptedData

'    End Function

'    Public Function encryptData_TrippleDES(rawData As String) As String

'        Dim rawDataBytes() As Byte = System.Convert.FromBase64String(rawData)
'        'Dim encDataBytes() As Byte = iEncryptData_TripleDES(rawDataBytes)
'        'Dim encDataStr As String = System.Convert.ToBase64String(encDataBytes)
'        Dim encDataStr As String = System.Convert.ToBase64String(rawDataBytes)

'        Return encDataStr

'    End Function

'    Public Function decryptData_TrippleDES(rawData As String) As String

'        Dim rawDataBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(rawData)
'        'Dim decDataBytes() As Byte = iDeccryptData_TripleDES(rawDataBytes)
'        'Dim decDataStr As String = System.Convert.ToBase64String(decDataBytes)
'        Dim decDataStr As String = System.Convert.ToBase64String(rawDataBytes)

'        Return decDataStr

'    End Function

'#End Region

'#Region "Private Procedures"

'    Private Function iEncryptData_TripleDES(rawData() As Byte) As Byte()

'        'prepare streams
'        Dim fin As New MemoryStream(rawData)
'        Dim fout As New MemoryStream
'        fout.SetLength(0)

'        'Create variables to help with read and write.
'        Dim bin(100) As Byte 'This is intermediate storage for the encryption.
'        Dim rdlen As Long = 0 'This is the total number of bytes written.
'        Dim totlen As Long = fin.Length 'This is the total length of the input file.
'        Dim len As Integer 'This is the number of bytes to be written at a time.
'        Dim tdes As New TripleDESCryptoServiceProvider()
'        Dim encStream As New CryptoStream(fout, tdes.CreateEncryptor(Key_TripleDES, IV_TripleDES), CryptoStreamMode.Write)

'        'Read from the input file, then encrypt and write to the output file.
'        While rdlen < totlen
'            len = fin.Read(bin, 0, 100)
'            encStream.Write(bin, 0, len)
'            rdlen = rdlen + len
'        End While

'        encStream.Close()

'        Return fout.ToArray
'    End Function

'    Private Function iDeccryptData_TripleDES(rawData() As Byte) As Byte()

'        'prepare streams
'        Dim fin As New MemoryStream(rawData)
'        Dim fout As New MemoryStream
'        fout.SetLength(0)

'        'Create variables to help with read and write.
'        Dim bin(100) As Byte 'This is intermediate storage for the encryption.
'        Dim rdlen As Long = 0 'This is the total number of bytes written.
'        Dim totlen As Long = fin.Length 'This is the total length of the input file.
'        Dim len As Integer 'This is the number of bytes to be written at a time.
'        Dim tdes As New TripleDESCryptoServiceProvider()
'        Dim decStream As New CryptoStream(fout, tdes.CreateDecryptor(Key_TripleDES, IV_TripleDES), CryptoStreamMode.Write)

'        'Read from the input file, then encrypt and write to the output file.
'        While rdlen < totlen
'            len = fin.Read(bin, 0, 100)
'            decStream.Write(bin, 0, len)
'            rdlen = rdlen + len
'        End While

'        decStream.Close()

'        Return fout.ToArray

'    End Function

'    Private Function encryptStringToBytes_AES(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
'        ' Check arguments.
'        If plainText Is Nothing OrElse plainText.Length <= 0 Then
'            Throw New ArgumentNullException("plainText")
'        End If
'        If Key Is Nothing OrElse Key.Length <= 0 Then
'            Throw New ArgumentNullException("Key")
'        End If
'        If IV Is Nothing OrElse IV.Length <= 0 Then
'            Throw New ArgumentNullException("IV")
'        End If

'        ' Declare the RijndaelManaged object
'        ' used to encrypt the data.
'        Dim aesAlg As RijndaelManaged = Nothing

'        ' Declare the stream used to encrypt to an in memory
'        ' array of bytes.
'        Dim msEncrypt As MemoryStream = Nothing

'        Try
'            ' Create a RijndaelManaged object
'            ' with the specified key and IV.
'            aesAlg = New RijndaelManaged()
'            aesAlg.Key = Key
'            aesAlg.IV = IV

'            ' Create a decrytor to perform the stream transform.
'            Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

'            ' Create the streams used for encryption.
'            msEncrypt = New MemoryStream()
'            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
'                Using swEncrypt As New StreamWriter(csEncrypt)

'                    'Write all data to the stream.
'                    swEncrypt.Write(plainText)
'                End Using
'            End Using
'        Catch ex As Exception
'            Throw ex
'        Finally
'            ' Clear the RijndaelManaged object.
'            If Not (aesAlg Is Nothing) Then
'                aesAlg.Clear()
'            End If
'        End Try
'        ' Return the encrypted bytes from the memory stream.
'        Return msEncrypt.ToArray()

'    End Function

'    Private Function decryptStringFromBytes_AES(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
'        ' Check arguments.
'        If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
'            Throw New ArgumentNullException("cipherText")
'        End If
'        If Key Is Nothing OrElse Key.Length <= 0 Then
'            Throw New ArgumentNullException("Key")
'        End If
'        If IV Is Nothing OrElse IV.Length <= 0 Then
'            Throw New ArgumentNullException("Key")
'        End If

'        ' Declare the RijndaelManaged object
'        ' used to decrypt the data.
'        Dim aesAlg As RijndaelManaged = Nothing

'        ' Declare the string used to hold
'        ' the decrypted text.
'        Dim plaintext As String = Nothing

'        Try
'            ' Create a RijndaelManaged object
'            ' with the specified key and IV.
'            aesAlg = New RijndaelManaged()
'            aesAlg.Key = Key
'            aesAlg.IV = IV

'            ' Create a decrytor to perform the stream transform.
'            Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

'            ' Create the streams used for decryption.
'            Using msDecrypt As New MemoryStream(cipherText)
'                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
'                    Using srDecrypt As New StreamReader(csDecrypt)

'                        ' Read the decrypted bytes from the decrypting stream
'                        ' and place them in a string.
'                        plaintext = srDecrypt.ReadToEnd()
'                    End Using
'                End Using
'            End Using
'        Catch ex As Exception
'            Throw ex
'        Finally

'            ' Clear the RijndaelManaged object.
'            If Not (aesAlg Is Nothing) Then
'                aesAlg.Clear()
'            End If
'        End Try
'        Return plaintext

'    End Function

'#End Region

'End Class
